//Scala编程实战 P253
//可遍历的集合常用方法

object collection1003 extends App {

  val list01 = List[Int](1, 2, 3, 4, 5)
  val list02 = List[Int](1, 2, 3, 4)
  val list03 = List[Int](1, 1, 1, 1, 4, 4, 4, 4)
  val list04 = List[Int](3, 6)
  val list05 = List("A,B,C", "D,E,F")
  val list06 = List(List("A,B,C"), List("D,E,F"))
  println(list01 collect { case (count) => count + 1 }) //List(2, 3, 4, 5, 6)   //https://www.jianshu.com/p/fa2ed7ed391e

  println(list01 count isTowDouble) //2  统计true的个数

  println(list01 diff list02) //List(5) 前者和后者不同的元素

  println(list01 drop 2) //List(3, 4, 5)
  println(list03.dropRight(4)) // List(1,1,1,1)
  println(list03 dropWhile (_ == 1)) // List(4,4,4,4)

  println(list01 filter isTowDouble) //List(2, 4)
  println(list01 filterNot isTowDouble) //List(1, 3, 5)

  println(list03 exists isThreeDouble) //flase
  println(list04 exists isThreeDouble) //true

  println(list02 find findFirstTowDouble) //Some(2)

  println(list05 flatten falttenDef) //List("A","B","C","D","E","F")
  println(list06 flatMap flatMapDef) //List("A","B","C","D","E","F")

  println(list01.fold(1)(_+_))
//  println(list01.foldLeft(1)())
//  println(list01.fold(1)(_+_))

  def isTowDouble(num: Int): Boolean = if (num % 2 == 0) true else false //谓词
  def isThreeDouble(num: Int): Boolean = if (num % 3 == 0) true else false //谓词
  def findFirstTowDouble(num: Int): Boolean = if (num % 2 == 0) true else false //谓词
  def falttenDef(str: String): List[String] = str.split(",").toList
  def flatMapDef(str: List[String]): List[String] = str.map(row => row.split(",")).flatten
  def printMk(str: String): String = str.mkString(",")

}
